package com.lib.controller.admin;

import com.lib.pojo.DTO.BookInformationDTO;
import com.lib.pojo.DTO.PageDTO;
import com.lib.pojo.VO.BookInformationVO;
import com.lib.pojo.VO.CategoryVO;
import com.lib.pojo.entity.BookInformation;
import com.lib.pojo.entity.PageResult;
import com.lib.pojo.entity.Result;
import com.lib.service.LibraryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@Tag(name = "图书管理端")
@RestController
@RequestMapping("/admin")
@Slf4j
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    //1：新增图书
    @PostMapping("/dish")
    @Operation(summary = "新增图书")
    public Result addLibrary(@RequestBody BookInformationDTO bookInformationdto){
        log.info("新增图书：{}",bookInformationdto.getBookName());
        libraryService.addLibrary(bookInformationdto);
        return Result.success();
    }
    //2：根据id查询套餐
    @GetMapping("/dish/{id}")
    @Operation(summary = "根据id查询套餐")
    public Result<BookInformationVO> getById(@PathVariable Integer id){
        log.info("根据id查询套餐：{}",id);
        BookInformationVO bookInformationVO = libraryService.getById(id);
        return Result.success(bookInformationVO);
    }
    //3：根据分类id查询图书
    @GetMapping("/dish/list")
    @Operation(summary = "根据分类id查询图书")
    public Result<List<CategoryVO>> getByCategoryId(Integer categoryId){
        log.info("根据分类id查询图书:{}",categoryId);
        List<CategoryVO> list = libraryService.getByCategoryId(categoryId);
        return Result.success(list);
    }
    //4：图书的起售、停售
    @Operation(summary = "图书的起售、停售")
    @PostMapping("/dish/status/{status}")
    public Result statusStartOrStop(@PathVariable Integer status,Integer id){
        log.info("图书的起售、停售。id为：{}",id);
        libraryService.statusStartOrStop(status,id);
        return Result.success();
    }
    //5:批量删除
    @Operation(summary = "批量删除")
    @DeleteMapping("/dish/ids")
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除分别是：{}",ids.toArray());
        libraryService.delete(ids);
        return Result.success();
    }
    //6:修改图书
    @Operation(summary = "修改图书")
    @PutMapping("/dish")
    public Result putBook(@RequestBody BookInformationVO bookInformationVO){
        log.info("修改图书的书名为：{}",bookInformationVO.getBookName());
        libraryService.putBook(bookInformationVO);
        return Result.success();
    }
    //7：图书分页查询
    @Operation(summary = "图书分页查询")
    @PostMapping("/dish/page")
    public Result<PageResult> page( @RequestBody PageDTO pageDTO){
        log.info("7:图书分页查询：{}",pageDTO);
        PageResult pageResult = libraryService.page(pageDTO);
        return Result.success(pageResult);
    }
    //8：根据id删除图书
    @Operation(summary = "根据id删除图书")
    @DeleteMapping("/dish/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("8:根据id删除图书：{}",id);
        libraryService.deleteById(id);
        return Result.success();
    }
    //9：将指定图书添加到套餐
    @Operation(summary = "将指定图书添加到套餐")
    @PostMapping("/dish/setmeal/{bookId}")
    public Result addDishAndSetmeal(Integer bookId,Integer recommendId){
        log.info("9:将指定图书添加到套餐：{}",bookId);
        libraryService.addDishAndSetmeal(bookId,recommendId);
        return Result.success();
    }
}
